#region

using System.Collections.Specialized;
using System.Text.RegularExpressions;
using CommandLineMapper.Interfaces;

#endregion

namespace CommandLineMapper {
    public class CommandLineSplitter : ICommandLineSplitter {
        private static readonly Regex regex =
            new Regex(@"\s+([-]{1,2}|[/])(?<arg>[a-z0-9?]+)([:=](?<val>[^\s""]+|""[^""]*""))?",
                      RegexOptions.IgnoreCase);

        #region Implementation of ICommandLineSplitter

        public NameValueCollection Split(string commandLine) {
            NameValueCollection collection = new NameValueCollection();
            if(string.IsNullOrEmpty(commandLine)) {
                return collection;
            }

            foreach (Match match in regex.Matches(commandLine)) {
                if(string.IsNullOrEmpty(match.Result("${val}"))) {
                    continue;
                }
                collection.Add(match.Result("${arg}"), match.Result("${val}"));
            }
            return collection;
        }

        #endregion
    }
}